
package com.insigma.business.qrcode.common._provider.impl;


import com.insigma.business.qrcode.common.dto.CommonRepresentationRecordBSearcher;
import com.insigma.business.qrcode.common.dto.CommonRepresentationRecordDO;
import com.insigma.business.qrcode.common._provider.BaseProviderImpl;
import com.insigma.business.qrcode.common._provider.CommonRepresentationRecordProvider;
import com.insigma.business.qrcode.common.converter.CommonRepresentationRecordProviderConverter;
import com.insigma.business.qrcode.common.service.CommonRepresentationRecordService;
import com.insigma.business.qrcode.common.template.BasePagingResponse;
import com.insigma.business.qrcode.common.template.area.ScenarioCodeEnum;
import com.insigma.business.qrcode.common.template.area.ServiceCallback;
import com.insigma.business.qrcode.common.util.BizCheckUtil;
import com.insigma.business.qrcode.common.util.ResponseUtils;
import com.insigma.business.qrcode.common.vo.BaseDeleteRequest;
import com.insigma.business.qrcode.common.vo.BaseResponse;
import com.insigma.business.qrcode.soldier.dto.CommonRepresentationRecordDTO;
import com.insigma.business.qrcode.soldier.request.BaseGetRequest;
import com.insigma.business.qrcode.soldier.request.CommonRepresentationRecordInsertRequest;
import com.insigma.business.qrcode.soldier.request.CommonRepresentationRecordSearchRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import java.util.List;


@Slf4j
@Service
@Component("commonRepresentationRecordProvider")
public class CommonRepresentationRecordProviderImpl extends BaseProviderImpl implements CommonRepresentationRecordProvider {

    @Autowired
    private CommonRepresentationRecordService commonRepresentationRecordService;


    @Override
    public BasePagingResponse<List<CommonRepresentationRecordDTO>> searchCommonRepresentationRecord(final CommonRepresentationRecordSearchRequest searchRequest) {

        final BasePagingResponse<List<CommonRepresentationRecordDTO>> basePagingResponse = new BasePagingResponse<>();
        serviceTemplate.execute(basePagingResponse, new ServiceCallback() {
            @Override
            public void check() {
                BizCheckUtil.checkNotNull(searchRequest, "查询分页CommonRepresentationRecord的请求参数不能为空");
            }

            @Override
            public void executeService() {
                final CommonRepresentationRecordBSearcher commonRepresentationRecordBSearcher = CommonRepresentationRecordProviderConverter.convertRequestSO2Searcher(searchRequest);
                final List<CommonRepresentationRecordDO> commonRepresentationRecordDOList = commonRepresentationRecordService.search(commonRepresentationRecordBSearcher);
                final List<CommonRepresentationRecordDTO> commonRepresentationRecordDTOList = CommonRepresentationRecordProviderConverter.convertResponseDOList2DTOList(commonRepresentationRecordDOList);
                ResponseUtils.buildSuccessPagingResponse(basePagingResponse, commonRepresentationRecordDTOList, searchRequest, commonRepresentationRecordBSearcher.getTotalCount());
            }

            @Override
            public ScenarioCodeEnum getScenarioEnum() {
                return ScenarioCodeEnum.COMMON_REPRESENTATION_RECORD_SEARCH;
            }
        });

        return basePagingResponse;
    }



    @Override
    public BaseResponse<CommonRepresentationRecordDTO> getCommonRepresentationRecord(final BaseGetRequest baseGetRequest) {

        final BaseResponse<CommonRepresentationRecordDTO> baseResponse = new BaseResponse<>();
        serviceTemplate.execute(baseResponse, new ServiceCallback() {
            @Override
            public void check() {
                BizCheckUtil.checkNotNull(baseGetRequest, "获取单条CommonRepresentationRecord的请求参数不能为空");
                BizCheckUtil.checkNotBlank((String) baseGetRequest.getId(), "id不能为空");
            }

            @Override
            public void executeService() {
                final CommonRepresentationRecordDO commonRepresentationRecordDO = commonRepresentationRecordService.get((String) baseGetRequest.getId());
                final CommonRepresentationRecordDTO remoteObj = CommonRepresentationRecordProviderConverter.convertResponseDO2DTO(commonRepresentationRecordDO);
                ResponseUtils.setupSuccessResponse(baseResponse, remoteObj);
            }

            @Override
            public ScenarioCodeEnum getScenarioEnum() {
                return ScenarioCodeEnum.COMMON_REPRESENTATION_RECORD_GET;
            }
        });
        return baseResponse;
    }


    @Override
    public BaseResponse<String> insertCommonRepresentationRecord(final CommonRepresentationRecordInsertRequest insertRequest) {

        final BaseResponse<String> baseResponse = new BaseResponse<>();
        serviceTemplate.execute(transactionTemplate, baseResponse, new ServiceCallback() {
            @Override
            public void check() {
                BizCheckUtil.checkNotNull(insertRequest, "新增CommonRepresentationRecord的请求参数不能为空");
            }

            @Override
            public void executeService() {
                final CommonRepresentationRecordDO commonRepresentationRecordDO = CommonRepresentationRecordProviderConverter.convertRequestIO2DO(insertRequest);
                commonRepresentationRecordService.insert(commonRepresentationRecordDO);
                ResponseUtils.setupSuccessResponse(baseResponse, commonRepresentationRecordDO.getId());
            }

            @Override
            public ScenarioCodeEnum getScenarioEnum() {
                return ScenarioCodeEnum.COMMON_REPRESENTATION_RECORD_INSERT;
            }
        });
        return baseResponse;
    }



    @Override
    public BaseResponse<Integer> deleteCommonRepresentationRecord(final BaseDeleteRequest deleteRequest) {

        final BaseResponse<Integer> baseResponse = new BaseResponse<>();
        serviceTemplate.execute(transactionTemplate, baseResponse, new ServiceCallback() {
            @Override
            public void check() {
                BizCheckUtil.checkNotNull(deleteRequest, "逻辑删除CommonRepresentationRecord的请求参数不能为空");
                BizCheckUtil.checkNotBlank((String) deleteRequest.getId(), "id不能为空");
                BizCheckUtil.checkNotBlank(deleteRequest.getModifierType(), "操作人类型不能为空");
                BizCheckUtil.checkNotBlank((String) deleteRequest.getModifierId(), "操作人不能为空");
            }

            @Override
            public void executeService() {
                final Integer affectRowNum = commonRepresentationRecordService.delete((String) deleteRequest.getId(), deleteRequest.getModifierType(), (String) deleteRequest.getModifierId());
                ResponseUtils.setupSuccessResponse(baseResponse, affectRowNum);
            }

            @Override
            public ScenarioCodeEnum getScenarioEnum() {
                return ScenarioCodeEnum.COMMON_REPRESENTATION_RECORD_DELETE_LOGICALLY;
            }
        });
        return baseResponse;
    }
}
